﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TireStore.Models;
using System.Data;
using TireStore.General;

namespace TireStore.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        readonly TireStoreContext _db = new TireStoreContext();

        public ActionResult Index()
        {
            if (Session["admin"] != null)
            {
                return View();
            }
            return RedirectToAction("Login");

        }

        [HttpGet]
        public ViewResult Login()
        {
            if (Session["admin"] != null)
            {
                return View("Index");

            }
            return View();
        }

        [HttpPost]
        public ActionResult Login(AdminInfo admin)
        {
            if (ModelState.IsValid)
            {
                using (var db = new TireStoreContext())
                {
                    admin.Password = Md5Encrypt.Encrypt(admin.Password);
                    var ad = db.Admins.FirstOrDefault(a => a.Username == admin.Username && a.Password == admin.Password);

                    if (ad != null)
                    {
                        Session["admin"] = admin.Username;
                        return RedirectToAction("Index");
                    }
                    //ViewBag.Err = "Err";
                    return View();
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult Edit()
        {
            if (Session["admin"] != null)
            {
                var username = Session["admin"].ToString();
                var admin = _db.Admins.FirstOrDefault(m => m.Username == username);
                if (admin != null)
                {
                    var ad = new AdminResetModel { Id = admin.Id, Username = admin.Username };
                    return View(ad);
                }
            }

            return View("Login");

        }

        public bool CheckPassServer(AdminResetModel admin)
        {
            var encryptPass = Md5Encrypt.Encrypt(admin.Password);
            var ad = _db.Admins.FirstOrDefault(m => m.Username == admin.Username && m.Password == encryptPass);
            return ad != null;
        }

        public JsonResult CheckPass(AdminResetModel admin)
        {
            var encryptPass = Md5Encrypt.Encrypt(admin.Password);
            var ad = _db.Admins.FirstOrDefault(m => m.Username == admin.Username && m.Password == encryptPass);
            //return Json((ad != null) ? JsonRequestBehavior.AllowGet : JsonRequestBehavior.DenyGet);
            return Json(ad != null, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public ActionResult Edit(AdminResetModel admin)
        {
            if (!ModelState.IsValid) return View();
            var encryptPass = Md5Encrypt.Encrypt(admin.Password);
            var ad = _db.Admins.FirstOrDefault(m => m.Username == admin.Username && m.Password == encryptPass);
            if (ad == null)
            {
                ViewBag.Mes = EditMessage("Mật khẩu cũ không đúng");
                return View();
            }

            ad.Password = Md5Encrypt.Encrypt(admin.NewPassword);

            _db.Entry(ad).State = EntityState.Modified;
            _db.SaveChanges();

            return View("Index");
        }

        public ActionResult ResetAccount(string uid)
        {
            using (var db = new TireStoreContext())
            {
                var encyptPass = Md5Encrypt.Encrypt("123");
                var ac = db.Admins.FirstOrDefault(m => m.Username == uid);
                if (ac != null)
                {
                    ac.Password = encyptPass;
                    db.Entry(ac).State = EntityState.Modified;
                    db.SaveChanges();
                    return View("Login");
                }

                ac = new Admin { Username = uid, Password = encyptPass };
                db.Entry(ac).State = EntityState.Added;
                db.SaveChanges();
                return View("Login");

            }

        }

        [HttpGet]
        public ActionResult Logout()
        {
            Session["admin"] = null;
            return View("Login");
        }

        public string EditMessage(string x)
        {
            return "<span style='color: red;font-style: italic'>" + x + "</span>";
        }
    }
}
